package com.cardone.platform.authority.service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.utils.EntityUtils;
import com.cardone.persistent.CommonDtoAttributes;
import com.cardone.platform.authority.dao.UsGrBusinessAreaDao;
import com.cardone.platform.authority.dao.UsGroupOrgDao;
import com.cardone.platform.authority.dao.UsGroupRoleDao;
import com.cardone.platform.authority.dao.UsGroupUserDao;
import com.cardone.platform.authority.dao.UserGrDao;
import com.cardone.platform.authority.dao.UserGroupDao;
import com.cardone.platform.authority.dto.UsGrBusinessAreaDto;
import com.cardone.platform.authority.dto.UsGroupOrgDto;
import com.cardone.platform.authority.dto.UsGroupRoleDto;
import com.cardone.platform.authority.dto.UsGroupUserDto;
import com.cardone.platform.authority.dto.UserGroupDto;

/**
 * 用户组服务
 * 
 * @author yaoht
 * 
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class UserGrDefaultService implements UserGrService {
	/**
	 * 用户组数据持久
	 */
	private UserGrDao userGrDao;

	/**
	 * 用户组数据持久
	 */
	private UserGroupDao userGroupDao;

	/**
	 * 用户组与业务范围数据持久
	 */
	private UsGrBusinessAreaDao usGrBusinessAreaDao;

	/**
	 * 用户组与组织数据持久
	 */
	private UsGroupOrgDao usGroupOrgDao;

	/**
	 * 用户组与角色数据持久
	 */
	private UsGroupRoleDao usGroupRoleDao;

	/**
	 * 用户组与用户数据持久
	 */
	private UsGroupUserDao usGroupUserDao;

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int deleteUserGroupByCode(@NonNull final UserGroupDto deleteUserGroup) {
		return this.userGroupDao.deleteByCode(deleteUserGroup, new String[] {});
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int deleteUserGroupByIds(@NonNull final UserGroupDto deleteUserGroup) {
		return this.userGroupDao.deleteByIds(deleteUserGroup);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int deleteUsGrBusinessAreaByCode(@NonNull final UsGrBusinessAreaDto deleteUsGrBusinessArea) {
		return this.usGrBusinessAreaDao.deleteByCode(deleteUsGrBusinessArea, new String[] {});
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int deleteUsGrBusinessAreaByIds(@NonNull final UsGrBusinessAreaDto deleteUsGrBusinessArea) {
		return this.usGrBusinessAreaDao.deleteByIds(deleteUsGrBusinessArea);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int deleteUsGroupOrgByCode(@NonNull final UsGroupOrgDto deleteUsGroupOrg) {
		return this.usGroupOrgDao.deleteByCode(deleteUsGroupOrg, new String[] {});
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int deleteUsGroupOrgByIds(@NonNull final UsGroupOrgDto deleteUsGroupOrg) {
		return this.usGroupOrgDao.deleteByIds(deleteUsGroupOrg);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int deleteUsGroupRoleByCode(@NonNull final UsGroupRoleDto deleteUsGroupRole) {
		return this.usGroupRoleDao.deleteByCode(deleteUsGroupRole, new String[] {});
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int deleteUsGroupRoleByIds(@NonNull final UsGroupRoleDto deleteUsGroupRole) {
		return this.usGroupRoleDao.deleteByIds(deleteUsGroupRole);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int deleteUsGroupUserByCode(@NonNull final UsGroupUserDto deleteUsGroupUser) {
		return this.usGroupUserDao.deleteByCode(deleteUsGroupUser, new String[] {});
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int deleteUsGroupUserByIds(@NonNull final UsGroupUserDto deleteUsGroupUser) {
		return this.usGroupUserDao.deleteByIds(deleteUsGroupUser);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public List<UserGroupDto> findListUserGroupByCode(@NonNull final UserGroupDto findListUserGroup) {
		return this.userGroupDao.findListByCode(UserGroupDto.class, findListUserGroup, new String[] {}, new String[] {});
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public List<UserGroupDto> findListUserGroupByName(@NonNull final UserGroupDto findListUserGroup) {
		return this.userGroupDao.findListByName(UserGroupDto.class, findListUserGroup);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public List<UsGrBusinessAreaDto> findListUsGrBusinessAreaByCode(@NonNull final UsGrBusinessAreaDto findListUsGrBusinessArea) {
		return this.usGrBusinessAreaDao.findListByCode(UsGrBusinessAreaDto.class, findListUsGrBusinessArea, new String[] {},
		        new String[] {});
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public List<UsGrBusinessAreaDto> findListUsGrBusinessAreaByName(@NonNull final UsGrBusinessAreaDto findListUsGrBusinessArea) {
		return this.usGrBusinessAreaDao.findListByName(UsGrBusinessAreaDto.class, findListUsGrBusinessArea);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public List<UsGroupOrgDto> findListUsGroupOrgByCode(@NonNull final UsGroupOrgDto findListUsGroupOrg) {
		return this.usGroupOrgDao.findListByCode(UsGroupOrgDto.class, findListUsGroupOrg, new String[] {}, new String[] {});
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public List<UsGroupOrgDto> findListUsGroupOrgByName(@NonNull final UsGroupOrgDto findListUsGroupOrg) {
		return this.usGroupOrgDao.findListByName(UsGroupOrgDto.class, findListUsGroupOrg);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public List<UsGroupRoleDto> findListUsGroupRoleByCode(@NonNull final UsGroupRoleDto findListUsGroupRole) {
		return this.usGroupRoleDao.findListByCode(UsGroupRoleDto.class, findListUsGroupRole, new String[] {}, new String[] {});
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public List<UsGroupRoleDto> findListUsGroupRoleByName(@NonNull final UsGroupRoleDto findListUsGroupRole) {
		return this.usGroupRoleDao.findListByName(UsGroupRoleDto.class, findListUsGroupRole);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public List<UsGroupUserDto> findListUsGroupUserByCode(@NonNull final UsGroupUserDto findListUsGroupUser) {
		return this.usGroupUserDao.findListByCode(UsGroupUserDto.class, findListUsGroupUser, new String[] {}, new String[] {});
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public List<UsGroupUserDto> findListUsGroupUserByName(@NonNull final UsGroupUserDto findListUsGroupUser) {
		return this.usGroupUserDao.findListByName(UsGroupUserDto.class, findListUsGroupUser);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public UserGroupDto findUserGroupByCode(@NonNull final UserGroupDto findUserGroup) {
		return this.userGroupDao.findByCode(UserGroupDto.class, findUserGroup, null, CommonDtoAttributes.id.name());
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public UserGroupDto findUserGroupById(@NonNull final UserGroupDto findUserGroup) {
		return this.userGroupDao.findByCode(UserGroupDto.class, findUserGroup, null, CommonDtoAttributes.id.name());
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public UsGrBusinessAreaDto findUsGrBusinessAreaByCode(@NonNull final UsGrBusinessAreaDto findUsGrBusinessArea) {
		return this.usGrBusinessAreaDao
		        .findByCode(UsGrBusinessAreaDto.class, findUsGrBusinessArea, null, CommonDtoAttributes.id.name());
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public UsGrBusinessAreaDto findUsGrBusinessAreaById(@NonNull final UsGrBusinessAreaDto findUsGrBusinessArea) {
		return this.usGrBusinessAreaDao
		        .findByCode(UsGrBusinessAreaDto.class, findUsGrBusinessArea, null, CommonDtoAttributes.id.name());
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public UsGroupOrgDto findUsGroupOrgByCode(@NonNull final UsGroupOrgDto findUsGroupOrg) {
		return this.usGroupOrgDao.findByCode(UsGroupOrgDto.class, findUsGroupOrg, null, CommonDtoAttributes.id.name());
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public UsGroupOrgDto findUsGroupOrgById(@NonNull final UsGroupOrgDto findUsGroupOrg) {
		return this.usGroupOrgDao.findByCode(UsGroupOrgDto.class, findUsGroupOrg, null, CommonDtoAttributes.id.name());
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public UsGroupRoleDto findUsGroupRoleByCode(@NonNull final UsGroupRoleDto findUsGroupRole) {
		return this.usGroupRoleDao.findByCode(UsGroupRoleDto.class, findUsGroupRole, null, CommonDtoAttributes.id.name());
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public UsGroupRoleDto findUsGroupRoleById(@NonNull final UsGroupRoleDto findUsGroupRole) {
		return this.usGroupRoleDao.findByCode(UsGroupRoleDto.class, findUsGroupRole, null, CommonDtoAttributes.id.name());
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public UsGroupUserDto findUsGroupUserByCode(@NonNull final UsGroupUserDto findUsGroupUser) {
		return this.usGroupUserDao.findByCode(UsGroupUserDto.class, findUsGroupUser, null, CommonDtoAttributes.id.name());
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public UsGroupUserDto findUsGroupUserById(@NonNull final UsGroupUserDto findUsGroupUser) {
		return this.usGroupUserDao.findByCode(UsGroupUserDto.class, findUsGroupUser, null, CommonDtoAttributes.id.name());
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<UserGroupDto> paginationUserGroupByCode(@NonNull final UserGroupDto paginationUserGroup) {
		return this.userGroupDao.paginationByCode(UserGroupDto.class, paginationUserGroup);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<Map<String, Object>> paginationUserGroupByName(@NonNull final UserGroupDto paginationUserGroup) {
		return this.userGroupDao.paginationByName(paginationUserGroup);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<UsGrBusinessAreaDto> paginationUsGrBusinessAreaByCode(
	        @NonNull final UsGrBusinessAreaDto paginationUsGrBusinessArea) {
		return this.usGrBusinessAreaDao.paginationByCode(UsGrBusinessAreaDto.class, paginationUsGrBusinessArea);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<Map<String, Object>> paginationUsGrBusinessAreaByName(
	        @NonNull final UsGrBusinessAreaDto paginationUsGrBusinessArea) {
		return this.usGrBusinessAreaDao.paginationByName(paginationUsGrBusinessArea);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<UsGroupOrgDto> paginationUsGroupOrgByCode(@NonNull final UsGroupOrgDto paginationUsGroupOrg) {
		return this.usGroupOrgDao.paginationByCode(UsGroupOrgDto.class, paginationUsGroupOrg);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<Map<String, Object>> paginationUsGroupOrgByName(@NonNull final UsGroupOrgDto paginationUsGroupOrg) {
		return this.usGroupOrgDao.paginationByName(paginationUsGroupOrg);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<UsGroupRoleDto> paginationUsGroupRoleByCode(@NonNull final UsGroupRoleDto paginationUsGroupRole) {
		return this.usGroupRoleDao.paginationByCode(UsGroupRoleDto.class, paginationUsGroupRole);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<Map<String, Object>> paginationUsGroupRoleByName(@NonNull final UsGroupRoleDto paginationUsGroupRole) {
		return this.usGroupRoleDao.paginationByName(paginationUsGroupRole);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<UsGroupUserDto> paginationUsGroupUserByCode(@NonNull final UsGroupUserDto paginationUsGroupUser) {
		return this.usGroupUserDao.paginationByCode(UsGroupUserDto.class, paginationUsGroupUser);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public PaginationDto<Map<String, Object>> paginationUsGroupUserByName(@NonNull final UsGroupUserDto paginationUsGroupUser) {
		return this.usGroupUserDao.paginationByName(paginationUsGroupUser);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public Integer readUserGroupByCodeNqId(@NonNull final UserGroupDto readUserGroup) {
		return this.userGroupDao.readByCodeNqId(Integer.class, readUserGroup);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public Integer readUserGroupByName(@NonNull final UserGroupDto readUserGroup) {
		return this.userGroupDao.readByName(Integer.class, null, readUserGroup);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public Integer readUsGrBusinessAreaByCodeNqId(@NonNull final UsGrBusinessAreaDto readUsGrBusinessArea) {
		return this.usGrBusinessAreaDao.readByCodeNqId(Integer.class, readUsGrBusinessArea);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public Integer readUsGrBusinessAreaByName(@NonNull final UsGrBusinessAreaDto readUsGrBusinessArea) {
		return this.usGrBusinessAreaDao.readByName(Integer.class, null, readUsGrBusinessArea);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public Integer readUsGroupOrgByCodeNqId(@NonNull final UsGroupOrgDto readUsGroupOrg) {
		return this.usGroupOrgDao.readByCodeNqId(Integer.class, readUsGroupOrg);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public Integer readUsGroupOrgByName(@NonNull final UsGroupOrgDto readUsGroupOrg) {
		return this.usGroupOrgDao.readByName(Integer.class, null, readUsGroupOrg);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public Integer readUsGroupRoleByCodeNqId(@NonNull final UsGroupRoleDto readUsGroupRole) {
		return this.usGroupRoleDao.readByCodeNqId(Integer.class, readUsGroupRole);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public Integer readUsGroupRoleByName(@NonNull final UsGroupRoleDto readUsGroupRole) {
		return this.usGroupRoleDao.readByName(Integer.class, null, readUsGroupRole);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public Integer readUsGroupUserByCodeNqId(@NonNull final UsGroupUserDto readUsGroupUser) {
		return this.usGroupUserDao.readByCodeNqId(Integer.class, readUsGroupUser);
	}

	@Override
	@Cacheable(value = UserGrService.BEAN_ID, key = com.cardone.common.cache.Constants.KEY_1)
	public Integer readUsGroupUserByName(@NonNull final UsGroupUserDto readUsGroupUser) {
		return this.usGroupUserDao.readByName(Integer.class, null, readUsGroupUser);
	}

	@Override
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public void reload() {
		this.userGrDao.reload();

		this.userGroupDao.reload();

		this.usGroupOrgDao.reload();

		this.usGrBusinessAreaDao.reload();

		this.usGroupRoleDao.reload();

		this.usGroupUserDao.reload();
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public UserGroupDto saveUserGroupByIdOrCode(@NonNull final UserGroupDto saveUserGroup) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(saveUserGroup, CommonDtoAttributes.code.name());

		final UserGroupDto oldUserGroup = this.userGroupDao.findByCode(UserGroupDto.class, saveUserGroup, null, whereAndEqProperties);

		if (oldUserGroup == null) {
			saveUserGroup.setId(UUID.randomUUID().toString());

			this.userGroupDao.insertByCode(saveUserGroup);

			final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(saveUserGroup);

			return this.userGroupDao.findByCode(UserGroupDto.class, saveUserGroup, null, notNullIdProperties);
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { CommonDtoAttributes.parentId.name(),
		        CommonDtoAttributes.typeId.name(), CommonDtoAttributes.code.name(), CommonDtoAttributes.name.name(),
		        CommonDtoAttributes.orderCode.name(), CommonDtoAttributes.value.name(), CommonDtoAttributes.remark.name() },
		        saveUserGroup);

		final String[] updateProperties = com.cardone.common.utils.BeanUtils.diffProperties(saveUserGroup, oldUserGroup, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return oldUserGroup;
		}

		final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(oldUserGroup);

		com.cardone.common.utils.BeanUtils.copyProperties(oldUserGroup, saveUserGroup, notNullIdProperties);

		this.userGroupDao.updateByCode(saveUserGroup, updateProperties, notNullIdProperties);

		return this.userGroupDao.findByCode(UserGroupDto.class, oldUserGroup, null, notNullIdProperties);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public UserGroupDto saveUserGroupByName(@NonNull final UserGroupDto saveUserGroup) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(saveUserGroup, CommonDtoAttributes.code.name());

		final UserGroupDto oldUserGroup = this.userGroupDao.findByCode(UserGroupDto.class, saveUserGroup, null, whereAndEqProperties);

		if (oldUserGroup == null) {
			saveUserGroup.setId(UUID.randomUUID().toString());

			this.userGroupDao.insertByCode(saveUserGroup);

			final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(saveUserGroup);

			return this.userGroupDao.findByCode(UserGroupDto.class, saveUserGroup, null, notNullIdProperties);
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { CommonDtoAttributes.parentId.name(),
		        CommonDtoAttributes.typeId.name(), CommonDtoAttributes.code.name(), CommonDtoAttributes.name.name(),
		        CommonDtoAttributes.orderCode.name(), CommonDtoAttributes.value.name(), CommonDtoAttributes.remark.name() },
		        saveUserGroup);

		final String[] updateProperties = com.cardone.common.utils.BeanUtils.diffProperties(saveUserGroup, oldUserGroup, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return oldUserGroup;
		}

		final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(oldUserGroup);

		com.cardone.common.utils.BeanUtils.copyProperties(oldUserGroup, saveUserGroup, notNullIdProperties);

		this.userGroupDao.updateByCode(saveUserGroup, updateProperties, notNullIdProperties);

		return this.userGroupDao.findByCode(UserGroupDto.class, oldUserGroup, null, notNullIdProperties);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public UsGrBusinessAreaDto saveUsGrBusinessAreaByIdOrCode(@NonNull final UsGrBusinessAreaDto saveUsGrBusinessArea) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(saveUsGrBusinessArea, CommonDtoAttributes.code.name());

		final UsGrBusinessAreaDto oldUsGrBusinessArea = this.usGrBusinessAreaDao.findByCode(UsGrBusinessAreaDto.class,
		        saveUsGrBusinessArea, null, whereAndEqProperties);

		if (oldUsGrBusinessArea == null) {
			saveUsGrBusinessArea.setId(UUID.randomUUID().toString());

			this.usGrBusinessAreaDao.insertByCode(saveUsGrBusinessArea);

			final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(saveUsGrBusinessArea);

			return this.usGrBusinessAreaDao.findByCode(UsGrBusinessAreaDto.class, saveUsGrBusinessArea, null, notNullIdProperties);
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { CommonDtoAttributes.parentId.name(),
		        CommonDtoAttributes.typeId.name(), CommonDtoAttributes.code.name(), CommonDtoAttributes.name.name(),
		        CommonDtoAttributes.orderCode.name(), CommonDtoAttributes.value.name(), CommonDtoAttributes.remark.name() },
		        saveUsGrBusinessArea);

		final String[] updateProperties = com.cardone.common.utils.BeanUtils.diffProperties(saveUsGrBusinessArea, oldUsGrBusinessArea,
		        useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return oldUsGrBusinessArea;
		}

		final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(oldUsGrBusinessArea);

		com.cardone.common.utils.BeanUtils.copyProperties(oldUsGrBusinessArea, saveUsGrBusinessArea, notNullIdProperties);

		this.usGrBusinessAreaDao.updateByCode(saveUsGrBusinessArea, updateProperties, notNullIdProperties);

		return this.usGrBusinessAreaDao.findByCode(UsGrBusinessAreaDto.class, oldUsGrBusinessArea, null, notNullIdProperties);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public UsGrBusinessAreaDto saveUsGrBusinessAreaByName(@NonNull final UsGrBusinessAreaDto saveUsGrBusinessArea) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(saveUsGrBusinessArea, CommonDtoAttributes.code.name());

		final UsGrBusinessAreaDto oldUsGrBusinessArea = this.usGrBusinessAreaDao.findByCode(UsGrBusinessAreaDto.class,
		        saveUsGrBusinessArea, null, whereAndEqProperties);

		if (oldUsGrBusinessArea == null) {
			saveUsGrBusinessArea.setId(UUID.randomUUID().toString());

			this.usGrBusinessAreaDao.insertByCode(saveUsGrBusinessArea);

			final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(saveUsGrBusinessArea);

			return this.usGrBusinessAreaDao.findByCode(UsGrBusinessAreaDto.class, saveUsGrBusinessArea, null, notNullIdProperties);
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { CommonDtoAttributes.parentId.name(),
		        CommonDtoAttributes.typeId.name(), CommonDtoAttributes.code.name(), CommonDtoAttributes.name.name(),
		        CommonDtoAttributes.orderCode.name(), CommonDtoAttributes.value.name(), CommonDtoAttributes.remark.name() },
		        saveUsGrBusinessArea);

		final String[] updateProperties = com.cardone.common.utils.BeanUtils.diffProperties(saveUsGrBusinessArea, oldUsGrBusinessArea,
		        useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return oldUsGrBusinessArea;
		}

		final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(oldUsGrBusinessArea);

		com.cardone.common.utils.BeanUtils.copyProperties(oldUsGrBusinessArea, saveUsGrBusinessArea, notNullIdProperties);

		this.usGrBusinessAreaDao.updateByCode(saveUsGrBusinessArea, updateProperties, notNullIdProperties);

		return this.usGrBusinessAreaDao.findByCode(UsGrBusinessAreaDto.class, oldUsGrBusinessArea, null, notNullIdProperties);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public UsGroupOrgDto saveUsGroupOrgByIdOrCode(@NonNull final UsGroupOrgDto saveUsGroupOrg) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(saveUsGroupOrg, CommonDtoAttributes.code.name());

		final UsGroupOrgDto oldUsGroupOrg = this.usGroupOrgDao.findByCode(UsGroupOrgDto.class, saveUsGroupOrg, null,
		        whereAndEqProperties);

		if (oldUsGroupOrg == null) {
			saveUsGroupOrg.setId(UUID.randomUUID().toString());

			this.usGroupOrgDao.insertByCode(saveUsGroupOrg);

			final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(saveUsGroupOrg);

			return this.usGroupOrgDao.findByCode(UsGroupOrgDto.class, saveUsGroupOrg, null, notNullIdProperties);
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { CommonDtoAttributes.parentId.name(),
		        CommonDtoAttributes.typeId.name(), CommonDtoAttributes.code.name(), CommonDtoAttributes.name.name(),
		        CommonDtoAttributes.orderCode.name(), CommonDtoAttributes.value.name(), CommonDtoAttributes.remark.name() },
		        saveUsGroupOrg);

		final String[] updateProperties = com.cardone.common.utils.BeanUtils.diffProperties(saveUsGroupOrg, oldUsGroupOrg,
		        useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return oldUsGroupOrg;
		}

		final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(oldUsGroupOrg);

		com.cardone.common.utils.BeanUtils.copyProperties(oldUsGroupOrg, saveUsGroupOrg, notNullIdProperties);

		this.usGroupOrgDao.updateByCode(saveUsGroupOrg, updateProperties, notNullIdProperties);

		return this.usGroupOrgDao.findByCode(UsGroupOrgDto.class, oldUsGroupOrg, null, notNullIdProperties);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public UsGroupOrgDto saveUsGroupOrgByName(@NonNull final UsGroupOrgDto saveUsGroupOrg) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(saveUsGroupOrg, CommonDtoAttributes.code.name());

		final UsGroupOrgDto oldUsGroupOrg = this.usGroupOrgDao.findByCode(UsGroupOrgDto.class, saveUsGroupOrg, null,
		        whereAndEqProperties);

		if (oldUsGroupOrg == null) {
			saveUsGroupOrg.setId(UUID.randomUUID().toString());

			this.usGroupOrgDao.insertByCode(saveUsGroupOrg);

			final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(saveUsGroupOrg);

			return this.usGroupOrgDao.findByCode(UsGroupOrgDto.class, saveUsGroupOrg, null, notNullIdProperties);
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { CommonDtoAttributes.parentId.name(),
		        CommonDtoAttributes.typeId.name(), CommonDtoAttributes.code.name(), CommonDtoAttributes.name.name(),
		        CommonDtoAttributes.orderCode.name(), CommonDtoAttributes.value.name(), CommonDtoAttributes.remark.name() },
		        saveUsGroupOrg);

		final String[] updateProperties = com.cardone.common.utils.BeanUtils.diffProperties(saveUsGroupOrg, oldUsGroupOrg,
		        useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return oldUsGroupOrg;
		}

		final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(oldUsGroupOrg);

		com.cardone.common.utils.BeanUtils.copyProperties(oldUsGroupOrg, saveUsGroupOrg, notNullIdProperties);

		this.usGroupOrgDao.updateByCode(saveUsGroupOrg, updateProperties, notNullIdProperties);

		return this.usGroupOrgDao.findByCode(UsGroupOrgDto.class, oldUsGroupOrg, null, notNullIdProperties);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public UsGroupRoleDto saveUsGroupRoleByIdOrCode(@NonNull final UsGroupRoleDto saveUsGroupRole) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(saveUsGroupRole, CommonDtoAttributes.code.name());

		final UsGroupRoleDto oldUsGroupRole = this.usGroupRoleDao.findByCode(UsGroupRoleDto.class, saveUsGroupRole, null,
		        whereAndEqProperties);

		if (oldUsGroupRole == null) {
			saveUsGroupRole.setId(UUID.randomUUID().toString());

			this.usGroupRoleDao.insertByCode(saveUsGroupRole);

			final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(saveUsGroupRole);

			return this.usGroupRoleDao.findByCode(UsGroupRoleDto.class, saveUsGroupRole, null, notNullIdProperties);
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { CommonDtoAttributes.parentId.name(),
		        CommonDtoAttributes.typeId.name(), CommonDtoAttributes.code.name(), CommonDtoAttributes.name.name(),
		        CommonDtoAttributes.orderCode.name(), CommonDtoAttributes.value.name(), CommonDtoAttributes.remark.name() },
		        saveUsGroupRole);

		final String[] updateProperties = com.cardone.common.utils.BeanUtils.diffProperties(saveUsGroupRole, oldUsGroupRole,
		        useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return oldUsGroupRole;
		}

		final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(oldUsGroupRole);

		com.cardone.common.utils.BeanUtils.copyProperties(oldUsGroupRole, saveUsGroupRole, notNullIdProperties);

		this.usGroupRoleDao.updateByCode(saveUsGroupRole, updateProperties, notNullIdProperties);

		return this.usGroupRoleDao.findByCode(UsGroupRoleDto.class, oldUsGroupRole, null, notNullIdProperties);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public UsGroupRoleDto saveUsGroupRoleByName(@NonNull final UsGroupRoleDto saveUsGroupRole) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(saveUsGroupRole, CommonDtoAttributes.code.name());

		final UsGroupRoleDto oldUsGroupRole = this.usGroupRoleDao.findByCode(UsGroupRoleDto.class, saveUsGroupRole, null,
		        whereAndEqProperties);

		if (oldUsGroupRole == null) {
			saveUsGroupRole.setId(UUID.randomUUID().toString());

			this.usGroupRoleDao.insertByCode(saveUsGroupRole);

			final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(saveUsGroupRole);

			return this.usGroupRoleDao.findByCode(UsGroupRoleDto.class, saveUsGroupRole, null, notNullIdProperties);
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { CommonDtoAttributes.parentId.name(),
		        CommonDtoAttributes.typeId.name(), CommonDtoAttributes.code.name(), CommonDtoAttributes.name.name(),
		        CommonDtoAttributes.orderCode.name(), CommonDtoAttributes.value.name(), CommonDtoAttributes.remark.name() },
		        saveUsGroupRole);

		final String[] updateProperties = com.cardone.common.utils.BeanUtils.diffProperties(saveUsGroupRole, oldUsGroupRole,
		        useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return oldUsGroupRole;
		}

		final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(oldUsGroupRole);

		com.cardone.common.utils.BeanUtils.copyProperties(oldUsGroupRole, saveUsGroupRole, notNullIdProperties);

		this.usGroupRoleDao.updateByCode(saveUsGroupRole, updateProperties, notNullIdProperties);

		return this.usGroupRoleDao.findByCode(UsGroupRoleDto.class, oldUsGroupRole, null, notNullIdProperties);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public UsGroupUserDto saveUsGroupUserByIdOrCode(@NonNull final UsGroupUserDto saveUsGroupUser) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(saveUsGroupUser, CommonDtoAttributes.code.name());

		final UsGroupUserDto oldUsGroupUser = this.usGroupUserDao.findByCode(UsGroupUserDto.class, saveUsGroupUser, null,
		        whereAndEqProperties);

		if (oldUsGroupUser == null) {
			saveUsGroupUser.setId(UUID.randomUUID().toString());

			this.usGroupUserDao.insertByCode(saveUsGroupUser);

			final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(saveUsGroupUser);

			return this.usGroupUserDao.findByCode(UsGroupUserDto.class, saveUsGroupUser, null, notNullIdProperties);
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { CommonDtoAttributes.parentId.name(),
		        CommonDtoAttributes.typeId.name(), CommonDtoAttributes.code.name(), CommonDtoAttributes.name.name(),
		        CommonDtoAttributes.orderCode.name(), CommonDtoAttributes.value.name(), CommonDtoAttributes.remark.name() },
		        saveUsGroupUser);

		final String[] updateProperties = com.cardone.common.utils.BeanUtils.diffProperties(saveUsGroupUser, oldUsGroupUser,
		        useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return oldUsGroupUser;
		}

		final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(oldUsGroupUser);

		com.cardone.common.utils.BeanUtils.copyProperties(oldUsGroupUser, saveUsGroupUser, notNullIdProperties);

		this.usGroupUserDao.updateByCode(saveUsGroupUser, updateProperties, notNullIdProperties);

		return this.usGroupUserDao.findByCode(UsGroupUserDto.class, oldUsGroupUser, null, notNullIdProperties);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public UsGroupUserDto saveUsGroupUserByName(@NonNull final UsGroupUserDto saveUsGroupUser) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(saveUsGroupUser, CommonDtoAttributes.code.name());

		final UsGroupUserDto oldUsGroupUser = this.usGroupUserDao.findByCode(UsGroupUserDto.class, saveUsGroupUser, null,
		        whereAndEqProperties);

		if (oldUsGroupUser == null) {
			saveUsGroupUser.setId(UUID.randomUUID().toString());

			this.usGroupUserDao.insertByCode(saveUsGroupUser);

			final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(saveUsGroupUser);

			return this.usGroupUserDao.findByCode(UsGroupUserDto.class, saveUsGroupUser, null, notNullIdProperties);
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { CommonDtoAttributes.parentId.name(),
		        CommonDtoAttributes.typeId.name(), CommonDtoAttributes.code.name(), CommonDtoAttributes.name.name(),
		        CommonDtoAttributes.orderCode.name(), CommonDtoAttributes.value.name(), CommonDtoAttributes.remark.name() },
		        saveUsGroupUser);

		final String[] updateProperties = com.cardone.common.utils.BeanUtils.diffProperties(saveUsGroupUser, oldUsGroupUser,
		        useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return oldUsGroupUser;
		}

		final String[] notNullIdProperties = EntityUtils.getNotNullIdProperties(oldUsGroupUser);

		com.cardone.common.utils.BeanUtils.copyProperties(oldUsGroupUser, saveUsGroupUser, notNullIdProperties);

		this.usGroupUserDao.updateByCode(saveUsGroupUser, updateProperties, notNullIdProperties);

		return this.usGroupUserDao.findByCode(UsGroupUserDto.class, oldUsGroupUser, null, notNullIdProperties);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int updateUserGroupByCode(@NonNull final UserGroupDto updateUserGroup) {
		return this.userGroupDao.updateByCode(updateUserGroup, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int updateUserGroupByIds(@NonNull final UserGroupDto updateUserGroup) {
		return this.userGroupDao.updateByIds(updateUserGroup);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int updateUsGrBusinessAreaByCode(@NonNull final UsGrBusinessAreaDto updateUsGrBusinessArea) {
		return this.usGrBusinessAreaDao.updateByCode(updateUsGrBusinessArea, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int updateUsGrBusinessAreaByIds(@NonNull final UsGrBusinessAreaDto updateUsGrBusinessArea) {
		return this.usGrBusinessAreaDao.updateByIds(updateUsGrBusinessArea);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int updateUsGroupOrgByCode(@NonNull final UsGroupOrgDto updateUsGroupOrg) {
		return this.usGroupOrgDao.updateByCode(updateUsGroupOrg, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int updateUsGroupOrgByIds(@NonNull final UsGroupOrgDto updateUsGroupOrg) {
		return this.usGroupOrgDao.updateByIds(updateUsGroupOrg);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int updateUsGroupRoleByCode(@NonNull final UsGroupRoleDto updateUsGroupRole) {
		return this.usGroupRoleDao.updateByCode(updateUsGroupRole, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int updateUsGroupRoleByIds(@NonNull final UsGroupRoleDto updateUsGroupRole) {
		return this.usGroupRoleDao.updateByIds(updateUsGroupRole);
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int updateUsGroupUserByCode(@NonNull final UsGroupUserDto updateUsGroupUser) {
		return this.usGroupUserDao.updateByCode(updateUsGroupUser, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	@CacheEvict(value = UserGrService.BEAN_ID, allEntries = true)
	public int updateUsGroupUserByIds(@NonNull final UsGroupUserDto updateUsGroupUser) {
		return this.usGroupUserDao.updateByIds(updateUsGroupUser);
	}
}